import {Effect, Model, SubscriptionsMapObject} from 'dva-core-ts';
import {Reducer} from 'redux';
import storage, {load} from '@/config/storage';
import axios from 'axios';
import {RootState} from '.';

const CATEGORY_URL = '/mock/11/category';

export interface ICategory {
  id: string;
  name: string;
  classify?: string;
}

interface CategoryModelState {
  isEdit: boolean;
  myCategorys: ICategory[];
  categorys: ICategory[];
}

interface CategoryModel extends Model {
  namespace: 'category';
  state: CategoryModelState;
  effects: {
    loadData: Effect;
    toggle: Effect;
  };
  reducers: {
    setState: Reducer<CategoryModelState>;
  };
  subscriptions: SubscriptionsMapObject;
}

const initialState = {
  isEdit: false,
  myCategorys: [
    {
      id: 'home',
      name: '推荐',
    },
    {
      id: 'vip',
      name: 'Vip',
    },
  ],
  categorys: [
    {
      id: '2',
      name: '小说',
      classify: '推荐',
    },
    {
      id: '3',
      name: '直播',
      classify: '推荐',
    },
    {
      id: '4',
      name: '广播',
      classify: '推荐',
    },
    {
      id: '5',
      name: '儿童',
      classify: '推荐',
    },
    {
      id: '6',
      name: '精品',
      classify: '推荐',
    },
    {
      id: '7',
      name: '畅销书',
      classify: '推荐',
    },
    {
      id: '8',
      name: '头条',
      classify: '推荐',
    },
    {
      id: '9',
      name: '武汉',
      classify: '推荐',
    },
    {
      id: '10',
      name: '70年',
      classify: '推荐',
    },
    {
      id: '11',
      name: '开学季',
      classify: '推荐',
    },
    {
      id: '12',
      name: '历史',
      classify: '知识',
    },
    {
      id: '13',
      name: '历史',
      classify: '知识',
    },
    {
      id: '14',
      name: '商业财经',
      classify: '知识',
    },
    {
      id: '15',
      name: '育儿百科',
      classify: '知识',
    },
    {
      id: '16',
      name: '人文',
      classify: '知识',
    },
    {
      id: '17',
      name: '英语',
      classify: '知识',
    },
    {
      id: '18',
      name: '个人成长',
      classify: '知识',
    },
    {
      id: '19',
      name: 'IT科技',
      classify: '知识',
    },
    {
      id: '20',
      name: '国学书院',
      classify: '知识',
    },
    {
      id: '21',
      name: '小语种',
      classify: '知识',
    },
    {
      id: '22',
      name: '名校公开课',
      classify: '知识',
    },
    {
      id: '23',
      name: '好书精讲',
      classify: '知识',
    },
    {
      id: '24',
      name: '少儿英语',
      classify: '知识',
    },
    {
      id: '25',
      name: '学科教育',
      classify: '知识',
    },
    {
      id: '26',
      name: '音乐',
      classify: '娱乐',
    },
    {
      id: '27',
      name: '相声',
      classify: '娱乐',
    },
    {
      id: '28',
      name: '段子',
      classify: '娱乐',
    },
    {
      id: '29',
      name: '情感生活',
      classify: '娱乐',
    },
    {
      id: '30',
      name: '广播剧',
      classify: '娱乐',
    },
    {
      id: '31',
      name: '娱乐',
      classify: '娱乐',
    },
    {
      id: '32',
      name: '影视',
      classify: '娱乐',
    },
    {
      id: '33',
      name: '二次元',
      classify: '娱乐',
    },
    {
      id: '34',
      name: '广播剧',
      classify: '娱乐',
    },
    {
      id: '35',
      name: '旅游',
      classify: '生活',
    },
    {
      id: '36',
      name: '健康养生',
      classify: '生活',
    },
    {
      id: '37',
      name: '时尚生活',
      classify: '生活',
    },
    {
      id: '38',
      name: '戏曲',
      classify: '生活',
    },
    {
      id: '39',
      name: '时尚生活',
      classify: '生活',
    },
  ],
};

const categoryModel: CategoryModel = {
  namespace: 'category',
  state: initialState,
  effects: {
    *loadData(_, {call, put}) {
      // 从storage中获取数据
      const myCategorys = yield call(load, {key: 'myCategorys'});
      const categorys = yield call(load, {key: 'categorys'});
      // 发起action，将数据保存到state
      if (myCategorys) {
        yield put({
          type: 'setState',
          payload: {
            myCategorys,
            categorys,
          },
        });
      } else {
        yield put({
          type: 'setState',
          payload: {
            categorys,
          },
        });
      }
    },
    *toggle({payload}, {put, select}) {
      const category = yield select(({category}: RootState) => category);
      yield put({
        type: 'setState',
        payload: {
          isEdit: !category.isEdit,
          myCategorys: payload.myCategorys,
        },
      });
      if (category.isEdit) {
        storage.save({
          key: 'myCategorys',
          data: payload.myCategorys,
        });
      }
    },
  },
  reducers: {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  subscriptions: {
    setup({dispatch}) {
      dispatch({type: 'loadDate'});
    },
    asyncStorage() {
      storage.sync.categorys = async () => {
        const {data} = await axios.get(CATEGORY_URL);
        return data;
      };
      storage.sync.myCategorys = async () => {
        return null;
      };
    },
  },
};

export default categoryModel;
